package com.feicheng.iou.config;

import com.feicheng.iou.constant.SessionConstant;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.result.ResultInfoV110;
import com.feicheng.iou.util.ReadPropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

@Component
public class LoginAuthInterceptor implements HandlerInterceptor {

    @Autowired
    HttpSession session;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        session = request.getSession();
        System.out.println(session.getId());
//        if (session.getAttribute(SessionConstant.CURRENT) == null) {
//            authFailOutput(response);
//            return false;
//        }
        return true;
    }

    //获取程序当前路径下的文件
    public static final Properties properties = ReadPropertiesUtil
            .getProperties("/information.properties");

    private void authFailOutput(HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(ResultInfoV110.failOrError(StatusCodeConstant.F003, (String) properties.get(StatusCodeConstant.F003)).toString());
        out.flush();
    }
}
